Add Tensor.to_sparse() API for sparse COO tensor conversion#1525
Add Tensor.to_sparse() API for sparse COO tensor conversion#1525alinpahontu2912 wants to merge 2 commits intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds support for converting dense tensors to sparse COO (Coordinate) format through the to_sparse() API, which is essential for implementing graph neural networks (GCN and GAT) that rely on sparse matrix operations. The implementation follows the standard TorchSharp pattern of adding bindings across all four layers: C++ native code, P/Invoke declarations, and managed C# methods.
Changes:
- Added two overloads of
to_sparse(): a parameterless version and one accepting a sparse dimension parameter - Implemented full binding chain from managed C# through P/Invoke to C++ native LibTorch calls
- Documentation added for both method overloads
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/TorchSharp/Tensor/Tensor.cs | Added managed C# methods to_sparse() and to_sparse(int sparse_dim) with XML documentation |
| src/TorchSharp/PInvoke/LibTorchSharp.THSTensor.cs | Added P/Invoke declarations for both native methods |
| src/Native/LibTorchSharp/THSTensor.h | Added C++ function declarations for sparse tensor conversion |
| src/Native/LibTorchSharp/THSTensor.cpp | Implemented C++ wrapper functions calling LibTorch's to_sparse() API |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add to_sparse() and to_sparse(int sparse_dim) methods to convert dense tensors to sparse COO format. This enables GCN and GAT graph neural network examples that require sparse matrix operations. Changes across all 4 binding layers: - THSTensor.h: declarations - THSTensor.cpp: implementations - LibTorchSharp.THSTensor.cs: P/Invoke - Tensor.cs: managed C# methods
b8bc6aa to
31eda5c
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add to_sparse() and to_sparse(int sparse_dim) methods to convert dense tensors to sparse COO format. This enables GCN and GAT graph neural network examples that require sparse matrix operations.
Changes across all 4 binding layers: